package com.fis.vaadin.format;

import java.text.Format;

import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;

@SuppressWarnings("serial")
public class PropertyFormatter implements Table.ColumnGenerator {

	private Format format;

	@Override
	public Object generateCell(Table source, Object itemId, Object columnId) {

		final Item item = source.getItem(itemId);
		final Property prop = item.getItemProperty(columnId);

		// Do not attempt to render NaN values
		final Property p = new BeanItem<Object>(prop.getValue())
				.getItemProperty("naN");
		if (p != null && p.getValue().equals(Boolean.TRUE)) {
			return null;
		}

		final String text = this.formatValue(prop.getValue());
		final Label label = new Label(text);

		label.setDescription(text);

		return label;

	}

	protected String formatValue(final Object value) {

		return this.format.format(value);

	}

	public PropertyFormatter(final Format format) {

		super();
		this.format = format;

	}

}
